﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;
using BusinessLogicLayer;
using DataAccessLayer;

namespace PresentationLayer
{
    public partial class ReportViewer : System.Web.UI.Page
    {
        GenerateReportController generateRptController = new GenerateReportController();
        ReportDocument rpt;
        List<object> deptByQty;
        List<object> deptByPrice;
        List<object> catByQty;
        List<object> catByPrice;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String generateBy = Request.QueryString["generatedBy"]; //Dept or Cat
                String filterBy = Request.QueryString["filterBy"]; //Price or Qty

                int num = Convert.ToInt32(Request.QueryString["num"]);

                if (num == 1)
                {
                    int fMonth = Convert.ToInt32(Request.QueryString["fm"]);
                    int fYear = Convert.ToInt32(Request.QueryString["fy"]);

                    if (generateBy == "Department")  //if the report is generated by Dept for one month
                    {
                        if (filterBy == "Quantity")
                        {
                            deptByQty = generateRptController.deptByQty(fMonth, fYear);
                            rpt = new crptEachDeptByQty();
                            rpt.SetDataSource(deptByQty);
                        }
                        else
                        {
                            deptByPrice = generateRptController.deptbyprice(fMonth, fYear);
                            rpt = new crptDeptByPrice();
                            rpt.SetDataSource(deptByPrice);
                        }

                    }
                    else  //if the report is generated by Cat
                    {
                        if (filterBy == "Quantity")
                        {
                            catByQty = generateRptController.catByQty(fMonth, fYear);
                            rpt = new crptCatByQty();
                            rpt.SetDataSource(catByQty);
                        }
                        else
                        {
                            catByPrice = generateRptController.catByPrice(fMonth, fYear);
                            rpt = new crptCatByPrice();
                            rpt.SetDataSource(catByPrice);
                        }
                    }
                }
                else if (num == 2)
                {
                    int fMonth = Convert.ToInt32(Request.QueryString["fm"]);
                    int fYear = Convert.ToInt32(Request.QueryString["fy"]);
                    int sMonth = Convert.ToInt32(Request.QueryString["sm"]);
                    int sYear = Convert.ToInt32(Request.QueryString["sy"]);

                    if (generateBy == "Department")  //if the report is generated by Dept for two months
                    {
                        if (filterBy == "Quantity")
                        {
                            rpt = new crptDeptByQtyTwo();
                            deptByQty = generateRptController.deptByQtyTwo(fMonth, fYear, sMonth, sYear);
                            rpt.SetDataSource(deptByQty);
                        }
                        else
                        {
                            deptByPrice = generateRptController.deptbypriceTwo(fMonth, fYear, sMonth, sYear);
                            rpt = new crptDeptByPriceTwo();
                            rpt.SetDataSource(deptByPrice);
                        }

                    }
                    else  //if the report is generated by Cat
                    {
                        if (filterBy == "Quantity")
                        {
                            catByQty = generateRptController.catByQtyTwo(fMonth, fYear, sMonth, sYear);
                            rpt = new crptCatByQtyTwo();
                            rpt.SetDataSource(catByQty);
                        }
                        else
                        {
                            catByPrice = generateRptController.catByPriceTwo(fMonth, fYear, sMonth, sYear);
                            rpt = new crptCatByPriceTwo();
                            rpt.SetDataSource(catByPrice);
                        }
                    }
                }
                else
                {
                    int fMonth = Convert.ToInt32(Request.QueryString["fm"]);
                    int fYear = Convert.ToInt32(Request.QueryString["fy"]);
                    int sMonth = Convert.ToInt32(Request.QueryString["sm"]);
                    int sYear = Convert.ToInt32(Request.QueryString["sy"]);
                    int tMonth = Convert.ToInt32(Request.QueryString["tm"]);
                    int tYear = Convert.ToInt32(Request.QueryString["ty"]);

                    if (generateBy == "Department")  //if the report is generated by Dept for three months
                    {

                        if (filterBy == "Quantity")
                        {
                            rpt = new crptDeptByQtyTwo();
                            deptByQty = generateRptController.deptByQtyThree(fMonth, fYear, sMonth, sYear, tMonth, tYear);
                            rpt.SetDataSource(deptByQty);
                        }
                        else
                        {
                            deptByPrice = generateRptController.deptByPriceThree(fMonth, fYear, sMonth, sYear, tMonth, tYear);
                            rpt = new crptDeptByPriceTwo();
                            rpt.SetDataSource(deptByPrice);
                        }

                    }

                    else  //if the report is generated by Cat
                    {
                        if (filterBy == "Quantity")
                        {
                            catByQty = generateRptController.catByQtyThree(fMonth, fYear, sMonth, sYear, tMonth, tYear);
                            rpt = new crptCatByQtyTwo();
                            rpt.SetDataSource(catByQty);
                        }
                        else
                        {
                            catByPrice = generateRptController.catByPriceThree(fMonth, fYear, sMonth, sYear, tMonth, tYear);
                            rpt = new crptCatByPriceTwo();
                            rpt.SetDataSource(catByPrice);
                        }
                    }



                }

                //ReportDocument rpt = new crptDeptByQtyTwo();
                //rpt.Load(Server.MapPath("~/testing.rpt"));
                //List<object> deptByQty = generateRptController.deptByQty(12, 2013);
                CrystalReportViewer1.ReportSource = rpt;
                CrystalReportViewer1.DataBind();
            }
            catch (Exception ex)
            {
            }

            
        }
    }
}